package com.hushui.mall.commodity.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hushui.mall.commodity.entity.AttrAttrgroupRelationEntity;
import com.hushui.mall.commodity.entity.AttrGroupEntity;
import com.hushui.mall.commodity.entity.ProductAttrValueEntity;
import com.hushui.mall.commodity.entity.vo.AttrVo;
import com.hushui.mall.commodity.entity.vo.AttrgVo;
import com.hushui.mall.commodity.entity.vo.AttrsVo;
import com.hushui.mall.commodity.service.AttrAttrgroupRelationService;
import com.hushui.mall.commodity.service.AttrGroupService;
import com.hushui.mall.commodity.service.ProductAttrValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.hushui.mall.commodity.entity.AttrEntity;
import com.hushui.mall.commodity.service.AttrService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;

import javax.servlet.http.HttpSession;

import static com.hushui.utils.AuthServerConstant.LOGIN_USER;


/**
 * 商品属性
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:46
 */
@RestController
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private ProductAttrValueService service;


    /**
     * 列表
     */
    @RequestMapping("commodity/attr/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/product/attr/base/list/{catelogId}")
    public R QueryAttrGroup(@PathVariable(name = "catelogId") Long catelogId, @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.queryPage(params, catelogId);
        return R.ok().put("page", pageUtils);
    }
    @GetMapping("/product/attr/base/listforspu/{spuId}")
    public R getSpuAttr(@PathVariable(name = "spuId") Long spuId){
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
        List<ProductAttrValueEntity> list = service.list(wrapper);
        return R.ok().put("data", list);
    }
    @RequestMapping("/product/attr/save")
    @Transactional
    public R addAttr(@RequestBody AttrVo vo){
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(vo,attrEntity);
        attrService.save(attrEntity);
        Long attrId = attrEntity.getAttrId();
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrId(attrId);
        attrAttrgroupRelationEntity.setAttrGroupId(vo.getAttrGroupId());
        attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        return R.ok();
    }
    @RequestMapping("/product/attrgroup/{attrgroupId}/attr/relation")
    @Transactional
    public R queryAttrList(@PathVariable(value = "attrgroupId") Long attrgroupId){
        QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityQueryWrapper = new QueryWrapper<>();
        attrAttrgroupRelationEntityQueryWrapper.eq("attr_group_id",attrgroupId);
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(attrAttrgroupRelationEntityQueryWrapper);
        ArrayList<AttrEntity> attrEntities = new ArrayList<>();
        for (AttrAttrgroupRelationEntity a :list
             ) {
            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id",a.getAttrId());
            AttrEntity one = attrService.getOne(wrapper);
            attrEntities.add(one);
        }

        return R.ok().put("data",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/product/attr/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityQueryWrapper = new QueryWrapper<>();
        attrAttrgroupRelationEntityQueryWrapper.eq("attr_id",attrId);
        AttrAttrgroupRelationEntity one = attrAttrgroupRelationService.getOne(attrAttrgroupRelationEntityQueryWrapper);
        AttrsVo attrsVo = new AttrsVo();
        BeanUtils.copyProperties(attr,attrsVo);
        attrsVo.setAttrGroupId(one.getAttrGroupId());
        attrsVo.setCatelogPath(attrService.getPath(attr.getCatelogId()));
        return R.ok().put("attr", attrsVo);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/attr/save")
    public R save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return R.ok();
    }
    @Transactional
    @RequestMapping("/product/attr/sale/list/{catelogId}")
    public R queryAttrGroupRelation(@PathVariable(name = "catelogId") Long catelogId, @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.queryVoPage(params, catelogId);

        return R.ok().put("page",pageUtils);
    }



    /**
     * 修改
     */
    @RequestMapping("/product/attr/update")
    public R update(@RequestBody AttrgVo vo){
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(vo,attrEntity);
        attrService.saveOrUpdate(attrEntity);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrGroupId(vo.getAttrGroupId());
        attrAttrgroupRelationEntity.setAttrId(vo.getAttrId());
        UpdateWrapper<AttrAttrgroupRelationEntity> updateWrapper= new UpdateWrapper<>();
        updateWrapper.eq("attr_id",vo.getAttrId());
        attrAttrgroupRelationService.update(attrAttrgroupRelationEntity,updateWrapper);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("commodity/attr/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
    @GetMapping("test")
    public R getSession(HttpSession session){
        System.out.println(session.getAttribute(LOGIN_USER).toString());
        return R.ok();
    }

}
